package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.dao.domain.PayVipPlan;
import com.yunkeji.dao.mapper.PayVipPlanMapper;
import com.yunkeji.dao.vo.PayVipPlanVo;
import com.yunkeji.system.domain.bo.PayVipPlanBo;
import com.yunkeji.system.service.IPayVipPlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * VIP方案Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class PayVipPlanServiceImpl implements IPayVipPlanService {

    private final PayVipPlanMapper baseMapper;

    /** 查询VIP方案 */
    @Override
    public PayVipPlanVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询VIP方案列表 */
    @Override
    public TableDataInfo<PayVipPlanVo> queryPageList(PayVipPlanBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PayVipPlan> lqw = buildQueryWrapper(bo);
        Page<PayVipPlanVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询VIP方案列表 */
    @Override
    public List<PayVipPlanVo> queryList(PayVipPlanBo bo) {
        LambdaQueryWrapper<PayVipPlan> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PayVipPlan> buildQueryWrapper(PayVipPlanBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PayVipPlan> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCnyPrice() != null, PayVipPlan::getCnyPrice, bo.getCnyPrice());
        lqw.eq(bo.getMonthNum() != null, PayVipPlan::getMonthNum, bo.getMonthNum());
        lqw.eq(bo.getPlanType() != null, PayVipPlan::getPlanType, bo.getPlanType());
        lqw.eq(bo.getDiscountFlag() != null, PayVipPlan::getDiscountFlag, bo.getDiscountFlag());
        lqw.eq(bo.getVipType() != null, PayVipPlan::getVipType, bo.getVipType());
        return lqw;
    }

    /** 新增VIP方案 */
    @Override
    public Boolean insertByBo(PayVipPlanBo bo) {
        PayVipPlan add = BeanUtil.toBean(bo, PayVipPlan.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改VIP方案 */
    @Override
    public Boolean updateByBo(PayVipPlanBo bo) {
        PayVipPlan update = BeanUtil.toBean(bo, PayVipPlan.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(PayVipPlan entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除VIP方案 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
